﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace Web.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult FormDataAction()
        {
            return View();
        }

        public ActionResult IframeAction()
        {
            return View();
        }

        public ActionResult BestIframeAction()
        {
            return View();
        }

        public ActionResult JqueryFormAction()
        {
            return View();
        }

        public ActionResult Upload()
        {
            return View();
        }

        public ActionResult SaveFileAjax(string author, IEnumerable<HttpPostedFileBase> file)
        {
            string result = null;
            try
            {
                foreach (var item in file)
                {
                    if (item != null)
                    {
                        string fileExtension = Path.GetExtension(item.FileName).ToLower();
                        item.SaveAs(Server.MapPath("~/upload/") + DateTime.Now.Ticks.ToString() + fileExtension);
                    }
                }
                result = "上传成功";
            }
            catch (Exception ex)
            {
                result = "上传失败";
            }
            return Json(new { Text = result });
        }

        [HttpPost]
        public ActionResult SaveFile(string author, IEnumerable<HttpPostedFileBase> file)
        {
            foreach (var item in file)
            {
                if (item != null)
                {
                    string fileExtension = Path.GetExtension(item.FileName).ToLower();
                    item.SaveAs(Server.MapPath("~/upload/") + DateTime.Now.Ticks.ToString() + fileExtension);
                }
            }
            return View("Upload");
        }
    }
}
